<template>
  <el-dialog
    v-model="dialogFormVisible"
    :close-on-click-modal="false"
    :show-close="false"
    width="1000px"
    @close="close"
  >
    <template #title>
      <div style="display: flex; justify-content: space-between">
        <div style="font-size: 16px">
          字典管理
          <B style="color: #a3a6ad"><vab-icon icon="arrow-right-s-line" /></B>
          工种字典{{ title }}
        </div>
        <vab-icon
          icon="close-circle-line"
          style="
            font-size: 24px;
            cursor: pointer;
            color: crimson;
            margin-top: -5px;
          "
          @click="close"
        />
      </div>
    </template>
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-row>
        <el-col :span="12">
          <el-form-item label="字典名称" prop="title">
            <el-input v-model.trim="form.title" :placeholder="''" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字典值" prop="value">
            <el-input v-model.trim="form.value" :placeholder="''" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button style="font-size: 16px" @click="close">取 消</el-button>
      <el-button
        style="
          background-color: #58b5e1;
          color: #fff;
          border: 0px;
          font-size: 14px;
        "
        @click="save"
      >
        {{ title == '添加' ? '保存' : '保存' }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { defineComponent, getCurrentInstance, reactive, toRefs } from 'vue'
  import { doEdit } from '@/api/special'

  export default defineComponent({
    name: 'TableEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const { proxy } = getCurrentInstance()
      const state = reactive({
        formRef: null,
        title: null,
        list: [],
        row: null,
        areaOptions: [],
        nationOptions: [],
        excelStatus: null,
        area: [],
        parents: [],
        genderStr: '根据身份证号解析',
        form: {},
        rules: {
          name: [
            { required: true, trigger: 'blur', message: '请输入区划名称' },
          ],
          nation: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入区划编码',
            },
          ],
          areaLevel: [
            {
              required: true,
              trigger: 'blur',
              message: '请选择区划级别',
            },
          ],
          pinyin: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入名称全拼',
            },
          ],
          szm: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入名称首拼',
            },
          ],
          letter: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入首字母大写',
            },
          ],
          code: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入区划编码',
            },
          ],
        },
        username: '',
        dialogFormVisible: false,
      })

      const showEdit = async (row) => {
        if (!row.id) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.form = Object.assign({}, row)
        }
        state.dialogFormVisible = true
      }

      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
        state.row = null
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            proxy.$baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
    mounted() {},
    methods: {},
  })
</script>
